 
import { UtilsHelper } from "utils/position/UtilsHelper";
import { APollingStation, APollingTask, CacheData, T_ID } from "./PollingStation";
import { CacheConstructureSiteTask, CacheDataConstructureSite } from "./PollingTasks/RoomItem/CacheConstructureSite";
import { CacheDataDepo, CacheDepoTask } from "./PollingTasks/RoomItem/CacheDepo";
import { CacheDataEvent, CacheEventTask } from "./PollingTasks/RoomItem/CacheEvent";
import { CacheDataHostiles, CacheHostilesTask } from "./PollingTasks/RoomItem/CacheHostiles";
import { CacheDataMinerial, CacheMinerialTask } from "./PollingTasks/RoomItem/CacheMinerial";
import {  CacheDataPowerBank, CachePowerBankTask } from "./PollingTasks/RoomItem/CachePowerBlink";
import { CacheDataSource, CacheSourceTask } from "./PollingTasks/RoomItem/CacheSource";
import { CacheDataStructure, CacheStructureTask } from "./PollingTasks/RoomItem/CacheStructure";
import { CacheBlueplan, CacheBlueplanTask } from "./PollingTasks/RoomItem/CacheBlueplan";
import { CacheDataResource, CacheResourceTask } from "./PollingTasks/RoomItem/CacheResource";

interface IPollTypes
{
    PowerBank:CacheDataPowerBank,
    Depo:CacheDataDepo,
    Minerial:CacheDataMinerial,
    Source:CacheDataSource,
    Structure:CacheDataStructure,
    HostileCreep:CacheDataHostiles,
    ConstructSite:CacheDataConstructureSite,
    Events:CacheDataEvent,
    Blueplan:CacheSourceTask
}
export class PollingStationManager
{
    private  PowerBanks:APollingStation<CacheDataPowerBank,CachePowerBankTask<CacheDataPowerBank>>;
    private  Depos:APollingStation<CacheDataDepo,CacheDepoTask<CacheDataDepo>>;
    private  Minerials:APollingStation<CacheDataMinerial,CacheMinerialTask<CacheDataMinerial>>;
    private  Sources:APollingStation<CacheDataSource,CacheSourceTask<CacheDataSource>>;
    private  Structures:APollingStation<CacheDataStructure,CacheStructureTask<CacheDataStructure>>;
    private  HostileCreep:APollingStation<CacheDataHostiles,CacheHostilesTask<CacheDataHostiles>>;
    private  ConstructSites:APollingStation<CacheDataConstructureSite,CacheConstructureSiteTask<CacheDataConstructureSite>>;
    private  Events:APollingStation<CacheDataEvent,CacheEventTask<CacheDataEvent>>;
    private  Blueplan:APollingStation<CacheBlueplan,CacheBlueplanTask>;

    private  Resource:APollingStation<CacheDataResource,CacheResourceTask>;

    private Stations!:Record<StationType,APollingStation<CacheData<ICacheData>,APollingTask<CacheData<ICacheData>>>>;


    private RegisterRooms:Set<string>;
    constructor()
    {
        this.RegisterRooms = new Set<string>();

        this.PowerBanks = new APollingStation();
        this.Depos = new APollingStation();
        this.Minerials = new APollingStation();
        this.Sources = new APollingStation();
        this.Structures = new APollingStation();
        this.HostileCreep = new APollingStation();
        this.ConstructSites = new APollingStation();
        this.Events = new APollingStation();
        this.Events = new APollingStation();
        this.Blueplan = new APollingStation();
        this.Resource  = new APollingStation();
        this.Stations =
            {
                "PowerBank":this.PowerBanks ,
                "Depo":this.Depos ,
                "Minerial":this.Minerials ,
                "Source":this.Sources ,
                "Structure":this.Structures,
                "ConstructSite":this.ConstructSites,
                "HostileCreep":this.HostileCreep,
                "Events": this.Events,
                "Blueplan": this.Blueplan,
                "Resource": this.Resource,
            };
    }
    public Update()
    {
        for(let key in Game.rooms)
        {
            if(!this.RegisterRooms.has(key))
            {
                this.RegisterRoom(key);
            }
        }
    }
    public tryRegisterRoom(roomName:string)
    {
        if(!this.RegisterRooms.has(roomName))
        {
            this.RegisterRoom(roomName);
        }
    }
    /**
     *
     * @param type
     * @param code
     * @param forceUpdate 是否强制更新缓存
     * @returns false 表示没有注册这个类型的任务 。成功后 返回数据或者undefined
     */
    public Poll<T extends CacheData<U>,U extends ICacheData >(type:StationType,code:T_ID,forceUpdate:boolean=false):T|undefined|false
    {
        const station = this.Stations[type];
        if(station)
        {
            return  station.Poll(code,forceUpdate) as (T|undefined);
        }
        return false;
    }
    public PollStructure(code:T_ID,forceUpdate:boolean=false):CacheDataStructure|undefined|false
    {
        return this.Poll("Structure",code,forceUpdate);
    }
    public PollSource(code:T_ID,forceUpdate:boolean=false):CacheDataSource|undefined|false
    {
        return this.Poll("Source",code,forceUpdate);
    }
    public PollMinerial(code:T_ID,forceUpdate:boolean=false):CacheDataMinerial|undefined|false
    {
        return this.Poll("Minerial",code,forceUpdate);
    }
    public PollPowerBank(code:T_ID,forceUpdate:boolean=false):CacheDataPowerBank|undefined|false
    {
        return this.Poll("PowerBank",code,forceUpdate);
    }
    public PollDepo(code:T_ID,forceUpdate:boolean=false):CacheDataDepo|undefined|false
    {
        return this.Poll("Depo",code,forceUpdate);
    }
    public PollHostiles(code:T_ID,forceUpdate:boolean=false):CacheDataHostiles|undefined|false
    {
        return this.Poll("HostileCreep",code,forceUpdate);
    }
    public PollConstructSites(code:T_ID,forceUpdate:boolean=false):CacheDataConstructureSite|undefined|false
    {
        // Memory.Debug[`poll${code}`] = Game.time;
        return this.Poll("ConstructSite",code,forceUpdate);
    }
    public PollEvents(code:T_ID,forceUpdate:boolean=false):CacheDataEvent|undefined|false
    {
        return this.Poll("Events",code,forceUpdate);
    }
    public PollBlueplan(code:T_ID,forceUpdate:boolean=false):CacheBlueplan|undefined|false
    {
        return this.Poll("Blueplan",code,forceUpdate);
    }
    public PollResource(code:T_ID,forceUpdate:boolean=false):CacheDataResource|undefined|false
    {
        return this.Poll("Resource",code,forceUpdate);
    }
    private RegisterTask(type:StationType,task:APollingTask<CacheData<ICacheData>>)
    {
        const station = this.Stations[type];
        if(station)
        {
            station.RegisterTask(task);
        }
    }

    private RegisterRoom(roomName:string)
    {
       
        if( UtilsHelper.IsHillway(roomName))
        {
            // 高速公路只关注。depo和pb
            const pbtask = new CachePowerBankTask(roomName);
            this.RegisterTask("PowerBank",pbtask);
            const depotask = new CacheDepoTask(roomName);
            this.RegisterTask("Depo",depotask);
            const eventtask = new CacheEventTask(roomName);
            this.RegisterTask("Events",eventtask);
            const HostileCreeptask = new CacheHostilesTask(roomName);
            this.RegisterTask("HostileCreep",HostileCreeptask);
            this.RegisterTask("Resource",new CacheResourceTask(roomName));
        }
        else
        {
            // 普通房间关注 source structure minerial
            const minerialtask = new CacheMinerialTask(roomName);
            this.RegisterTask("Minerial",minerialtask);
            const sourcetask = new CacheSourceTask(roomName);
            this.RegisterTask("Source",sourcetask);
            const Structuretask = new CacheStructureTask(roomName);
            this.RegisterTask("Structure",Structuretask);

            const sitetask = new CacheConstructureSiteTask(roomName);
            this.RegisterTask("ConstructSite",sitetask);

            const HostileCreeptask = new CacheHostilesTask(roomName);
            this.RegisterTask("HostileCreep",HostileCreeptask);

            const eventtask = new CacheEventTask(roomName);
            this.RegisterTask("Events",eventtask);
            
            const blueplantask = new CacheBlueplanTask(roomName);
            this.RegisterTask("Blueplan",blueplantask);

            this.RegisterTask("Resource",new CacheResourceTask(roomName));
        }
        this.RegisterRooms.add(roomName);
    }

}
